package com.nangle.managehost.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nangle.managehost.R;
import com.nangle.managehost.entity.Remind;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by YFB on 2017/3/24.
 */

public class RemindAdapter extends BaseAdapter{
    private LayoutInflater mInflater;
    private Context mContext;
    private List<Remind> reminds = new ArrayList<>();
    public RemindAdapter(Context context,List<Remind> reminds){
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.reminds = reminds;
    }
    @Override
    public int getCount() {
        return reminds.size();
    }

    @Override
    public Object getItem(int position) {
        return reminds.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.layout_remind_item, parent,false);
            viewHolder = new ViewHolder();
            viewHolder.tvRemindContent = (TextView)convertView.findViewById(R.id.remind_content);
            viewHolder.tvRemindTime = (TextView)convertView.findViewById(R.id.remind_time);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Remind remind = reminds.get(position);
        viewHolder.tvRemindContent.setText(remind.getContent());
        if(formatDateTime(remind.getTime())){
            viewHolder.tvRemindTime.setText("new");
            viewHolder.tvRemindTime.setBackgroundResource(R.drawable.light_blue_bg);
            viewHolder.tvRemindTime.setTextColor(mContext.getResources().getColor(R.color.colorwrite));
        }else{
            viewHolder.tvRemindTime.setBackgroundColor(mContext.getResources().getColor(R.color.colorwrite));
            viewHolder.tvRemindTime.setText(remind.getTime());
            viewHolder.tvRemindTime.setTextColor(mContext.getResources().getColor(R.color.text_color));
        }
        return convertView;
    }
    private final class ViewHolder {
        private TextView tvRemindContent;
        private TextView tvRemindTime;
    }
    /**
     * 格式化时间 判断日期是否是今天
     *
     * @param time
     * @return
     */
    private static boolean formatDateTime(String time) {
        SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
        if (time == null || "".equals(time)) {
            return false;
        }
        Date date = null;

        try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar current = Calendar.getInstance();
        current.setTime(date);
        Date datetime = new Date(System.currentTimeMillis());
        if(datetime!=null){
            Calendar current1 = Calendar.getInstance();
            current1.setTime(datetime);
            if(current.get(Calendar.YEAR)==current1.get(Calendar.YEAR)&&current.get(Calendar.MONTH)==current1.get(Calendar.MONTH)&&current.get(Calendar.DAY_OF_MONTH)==current1.get(Calendar.DAY_OF_MONTH)){
                return true;
            }else{
                return false;
            }

        }else{
            Calendar current1 = Calendar.getInstance();
            Calendar today = Calendar.getInstance(); // 今天
            today.set(Calendar.YEAR, current1.get(Calendar.YEAR));
            today.set(Calendar.MONTH, current1.get(Calendar.MONTH));
            today.set(Calendar.DAY_OF_MONTH, current1.get(Calendar.DAY_OF_MONTH));
            if(current.get(Calendar.YEAR)==today.get(Calendar.YEAR)&&current.get(Calendar.MONTH)==today.get(Calendar.MONTH)&&current.get(Calendar.DAY_OF_MONTH)==today.get(Calendar.DAY_OF_MONTH)){
                return true;
            }else {
                return false;
            }
        }



    }
}
